const express=require("express");
const router=express.Router();
const pool=require("../pool");
//var app = express();
//功能六:发表评论
//1:用户post请求 /addcomment                           
router.post("/news",(req,res)=>{
  //console.log(req)
  //2:获取二个参数 nid content
  let nid=req.body.nid;        //新闻编号
  let content=req.body.content;//评论内容
  let uid = req.body.uid;
  let uname
  new Promise((resolve,reject)=>{
    let sql =  `SELECT uname from yhc_user WHERE id = ?`;
    pool.query(sql,[uid],(err,result)=>{
      if(err) throw err;
      uname = result[0].uname;
      resolve(true);
    })
  }).then(value=>{
    //3:创建sql语句
    let sql="INSERT INTO news_comment VALUES";
        sql+="(null,?,'http://localhost:5055/img/logo.png',?,now(),0,?)";
    //4:发送sql语句并且返回返回结果
    pool.query(sql,[nid,uname,content],(err,result)=>{
      if(err)throw err;
      //判断执行insert语句影响行数
      if(result.affectedRows>0){
        res.send({code:1,msg:"评论发表成功"});
      }else{
        res.send({code:-1,msg:"评论发表失败"});
      }
    })
  })
})

router.post("/v",(req,res)=>{
  //console.log(req)
  //2:获取二个参数 nid content
  let nid=req.body.did;        //新闻编号
  let content=req.body.content;//评论内容
  let uid = req.body.uid;
  //console.log(nid);
  let uname
  new Promise((resolve,reject)=>{
    let sql =  `SELECT uname from yhc_user WHERE id = ?`;
    pool.query(sql,[uid],(err,result)=>{
      if(err) throw err;
      uname = result[0].uname;
      resolve(true);
    })
  }).then(value=>{
    //3:创建sql语句
    let sql="INSERT INTO video_comment VALUES";
        sql+="(null,?,null,?,now(),0,?)";
    //4:发送sql语句并且返回返回结果
    pool.query(sql,[nid,uname,content],(err,result)=>{
      if(err)throw err;
      //判断执行insert语句影响行数
      if(result.affectedRows>0){
        res.send({code:1,msg:"评论发表成功"});
      }else{
        res.send({code:-1,msg:"评论发表失败"});
      }
    })
  })
})
module.exports=router;